package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐接口信息")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐接口")
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页多少数据", required = true),
            @ApiImplicitParam(name = "name", value = "查询套餐名称", required = false)
    })
    public R<Page<SetmealDto>> getSetmealPage(int page, int pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.getSetmealPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除套餐接口")
    public R<String> deleteSetmeal(@RequestParam("ids") List<Long> ids) {
        setmealService.deleteSetmeal(ids);
        return R.success("删除成功");
    }

    @PutMapping("/status/{status}")
    @ApiOperation("修改套餐状态接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "要修改的状态", required = true),
            @ApiImplicitParam(name = "ids", value = "要修改状态的套餐id集合", required = true)})
    public R<String> setmealStatusByStatus(@PathVariable Integer status, @RequestParam("ids") List<Long> ids) {
        setmealService.setmealStatusByStatus(status, ids);
        if (status == 1) {
            return R.success("启用成功");
        }
        return R.success("禁用成功");
    }

    @GetMapping("/{id}")
    @ApiOperation("修改套餐数据回显接口")
    public R<SetmealDto> querySetmealById(@PathVariable Long id) {
        log.info("修改回显id:{}", id);
        SetmealDto setmealDto = setmealService.querySetmealById(id);
        return R.success(setmealDto);
    }

    @PutMapping
    @ApiOperation("保存修改套餐接口")
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto) {
        setmealService.editSetmeal(setmealDto);
        return R.success("保存成功");
    }

    //根据条件查询套餐中的菜品
    @GetMapping("/list")
    @ApiOperation("条件查询套餐接口")
    public R<List<Setmeal>> querySetmealList(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealService.queryDishList(setmeal);
        return R.success(setmeals);
    }

    //套餐显示菜品
    @GetMapping("/dish/{id}")
    @ApiOperation("显示套餐菜品接口")
    public R<List<SetmealDish>> setMealDishDetails(@PathVariable Long id) {
        List<SetmealDish> setmealDishes = setmealService.setMealDishDetails(id);
        return R.success(setmealDishes);
    }
}
